
package src.main.java.com.pms.gui;

import src.main.java.com.pms.gui.dialog.AddPetDialog;
import src.main.java.com.pms.gui.dialog.PetListDialog;
import src.main.java.com.pms.gui.dialog.SearchResultDialog;
import src.main.java.com.pms.gui.dialog.appointment.AppointmentListDialog;
import src.main.java.com.pms.gui.dialog.appointment.BathAppointmentDialog;
import src.main.java.com.pms.gui.dialog.appointment.CremationAppointmentDialog;
import src.main.java.com.pms.gui.dialog.appointment.VaccineAppointmentDialog;
import src.main.java.com.pms.gui.dialog.sale.CancelSaleDialog;
import src.main.java.com.pms.gui.dialog.sale.SaleListDialog;
import src.main.java.com.pms.gui.dialog.sale.SetSaleDialog;
import src.main.java.com.pms.gui.dialog.sale.UpdateSalePriceDialog;
import src.main.java.com.pms.gui.dialog.stats.AgeStatsDialog;
import src.main.java.com.pms.gui.dialog.stats.TypeStatsDialog;
import src.main.java.com.pms.gui.dialog.user.ChangePasswordDialog;
import src.main.java.com.pms.gui.dialog.user.ChangeUsernameDialog;
import src.main.java.com.pms.service.PetBasicService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

public class MainFrame extends JFrame {
    private PetBasicService petService;
    private String currentUsername; // 当前用户名

    public MainFrame(String username) { // 构造函数接受用户名参数
        this.currentUsername = username;
        initComponents();
        this.petService = new PetBasicService();
    }

    private void initComponents() {
        setTitle("宠物管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setLocationRelativeTo(null);

        // 设置现代风格的外观
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建菜单栏
        JMenuBar menuBar = createMenuBar();
        setJMenuBar(menuBar);

        // 创建欢迎面板
        JPanel welcomePanel = createWelcomePanel();
        mainPanel.add(welcomePanel, BorderLayout.CENTER);

        add(mainPanel);
    }

    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // 宠物管理菜单
        JMenu petMenu = new JMenu("宠物管理");
        addMenuItem(petMenu, "添加宠物", e -> showAddPetDialog());
        addMenuItem(petMenu, "宠物列表", e -> showPetList());
        addMenuItem(petMenu, "搜索宠物", e -> showSearchDialog());
        menuBar.add(petMenu);

        // 预约服务菜单
        JMenu serviceMenu = new JMenu("预约服务");
        addMenuItem(serviceMenu, "洗澡预约", e -> showBathAppointmentDialog());
        addMenuItem(serviceMenu, "疫苗预约", e -> showVaccineAppointmentDialog());
        addMenuItem(serviceMenu, "火化预约", e -> showCremationAppointmentDialog());
        addMenuItem(serviceMenu, "查看预约", e -> showAppointmentListDialog());
        menuBar.add(serviceMenu);

        // 寄卖管理菜单
        JMenu saleMenu = new JMenu("寄卖管理");
        addMenuItem(saleMenu, "设置寄卖", e -> showSetSaleDialog());
        addMenuItem(saleMenu, "寄卖列表", e -> showSaleListDialog());
        menuBar.add(saleMenu);

        // 统计分析菜单
        JMenu statsMenu = new JMenu("统计分析");
        addMenuItem(statsMenu, "宠物年龄统计", e -> showAgeStatsDialog());
        addMenuItem(statsMenu, "宠物类型统计", e -> showTypeStatsDialog());
        menuBar.add(statsMenu);

        // 添加“退出登录”按钮
        JMenu accountMenu = new JMenu("账户");
        addMenuItem(accountMenu, "修改用户名", e -> showChangeUsernameDialog());
        addMenuItem(accountMenu, "修改密码", e -> showChangePasswordDialog());
        addMenuItem(accountMenu, "退出登录", e -> logout());
        menuBar.add(accountMenu);

        return menuBar;
    }

    private void showChangePasswordDialog() {
        new ChangePasswordDialog(this).setVisible(true);
    }

    private void showChangeUsernameDialog() {
        new ChangeUsernameDialog(this).setVisible(true);
    }

    private void addMenuItem(JMenu menu, String text, ActionListener listener) {
        JMenuItem item = new JMenuItem(text);
        item.addActionListener(listener);
        menu.add(item);
    }

    private JPanel createWelcomePanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(new Color(240, 240, 240));

        JLabel welcomeLabel = new JLabel("欢迎使用宠物管理系统");
        welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        welcomeLabel.setForeground(new Color(51, 51, 51));

        JLabel userLabel = new JLabel("当前用户: " + currentUsername);
        userLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        userLabel.setForeground(new Color(51, 51, 51));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(welcomeLabel, gbc);

        gbc.gridy = 1;
        panel.add(userLabel, gbc);

        return panel;
    }

    private void showAddPetDialog() {
        new AddPetDialog(this).setVisible(true);
    }

    private void showPetList() {
        new PetListDialog(this).setVisible(true);
    }

    private void showSearchDialog() {
        String searchName = JOptionPane.showInputDialog(this,
                "请输入要搜索的宠物名称：", "搜索宠物",
                JOptionPane.QUESTION_MESSAGE);

        if (searchName != null && !searchName.trim().isEmpty()) {
            try {
                ResultSet rs = petService.searchPetsByName(searchName.trim());
                new SearchResultDialog(this, rs, searchName.trim()).setVisible(true);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "搜索失败：" + e.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // 预约服务菜单相关方法
    private void showBathAppointmentDialog() {
        new BathAppointmentDialog(this).setVisible(true);
    }

    private void showVaccineAppointmentDialog() {
        new VaccineAppointmentDialog(this).setVisible(true);
    }

    private void showCremationAppointmentDialog() {
        new CremationAppointmentDialog(this).setVisible(true);
    }

    private void showAppointmentListDialog() {
        new AppointmentListDialog(this).setVisible(true);
    }

    // 寄卖管理菜单相关方法
    private void showSetSaleDialog() {
        new SetSaleDialog(this).setVisible(true);
    }

    private void showSaleListDialog() {
        new SaleListDialog(this).setVisible(true);
    }

    private void showCancelSaleDialog() {
        new CancelSaleDialog(this).setVisible(true);
    }

    private void showUpdateSalePriceDialog() {
        new UpdateSalePriceDialog(this).setVisible(true);
    }

    // 统计分析菜单相关方法
    private void showAgeStatsDialog() {
        new AgeStatsDialog(this).setVisible(true);
    }

    private void showTypeStatsDialog() {
        new TypeStatsDialog(this).setVisible(true);
    }

    // 退出登录的方法
    private void logout() {
        this.dispose(); // 关闭当前主界面
        new LoginFrame().setVisible(true); // 重新显示登录界面
    }
}
